<?php
/**
 * Created by IDEA.
 * User: dinesh
 * Date: Apr 12, 2012
 * Time: 6:36:36 PM
 * To change this template use File | Settings | File Templates.
 */
 
class Privilege {
    private $db;

    public function __construct(){
        $this->db = new MysqlImprovedDriver;
        $this->db->connect();
    }
    public function getAllPrivileges(){
        $privileges = array();
        //get privileges
        $sql = "SELECT id ,code FROM aea_privilege WHERE status=1 AND is_delete=0" ;
        $this->db->prepare($sql);
        $this->db->query();
        while ($out = $this->db->fetch()) {
            $privileges[$out->id] =$out->code;
        }
        return $privileges;
    }
    public function getUserPrivileges($userId = 0){
        $userPrivileges = array();
        //get user privileges
        $sql = "select distinct privilege_id as pid from aea_role_privilege where role_id in (select role_id from aea_user_role where user_id =$userId AND is_delete=0 AND aea_user_role.role_id in (select id from aea_role where status =1) ) AND is_delete=0 AND status=1" ;
        $this->db->prepare($sql);
        $this->db->query();
        while ($out = $this->db->fetch()) {
            $userPrivileges[$out->pid] =$out->pid;
        }
        return $userPrivileges;
    }
    public function getPermissionPrivilege($userId){
         $permissionPrivilege = array();
         $allPrivi = $this->getAllPrivileges();
         $userPriviId = $this->getUserPrivileges($userId);
         foreach($allPrivi as $key=>$value){
             if(isset($userPriviId[$key])){
                $permissionPrivilege[$value] = 1;
             }else{
                  $permissionPrivilege[$value] = 0;
             }
         }
        return $permissionPrivilege;
    }

}
